﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;
using Blacklight.Silverlight.Controls;


namespace BlackLight
{
    public partial class Page : UserControl
    { 
        public Page(string PoshCharts)
        {
            InitializeComponent();  

            foreach (string SingleChart in PoshCharts.Split('§'))
            {
                if (SingleChart != "")
                {                    
                    // Create a new Visifire Chart
                    Chart BlackChart = new Chart(); 
                    
                    ////Add Title
                    //Title title = new Title();
                    //title.Text = SingleChart.Split('£')[0];
                    //BlackChart.Titles.Add(title);
                    string strChartSetup = SingleChart.Split('£')[0];
                    string strChartSeriesType = SingleChart.Split('£')[1];                    
                    string strChartSeriesName = SingleChart.Split('£')[2];
                    string strChartDataName = SingleChart.Split('£')[3];
                    string strChartData = SingleChart.Split('£')[4];
                    
                    int intNbrSeries = strChartSeriesType.Split('µ').Count() - 1;

                    if (strChartSetup.Split('µ')[1] != "")
                    {
                        Axis axisX = new Axis();
                        axisX.Title = strChartSetup.Split('µ')[1];
                        BlackChart.AxesX.Add(axisX);
                    }

                    if (strChartSetup.Split('µ')[2] != "")
                    {
                        Axis axisY = new Axis();
                        axisY.Title = strChartSetup.Split('µ')[2];
                        BlackChart.AxesY.Add(axisY);
                    }

                    for (int i = 0; i < intNbrSeries; i++)
                    {
                        DataSeries dataSeries = new DataSeries();
                        string SerieType = strChartSeriesType.Split('µ')[i];
                        switch (SerieType)
                        {
                            case "Area":
                                dataSeries.RenderAs = RenderAs.Area;
                                break;
                            case "Bar":
                                dataSeries.RenderAs = RenderAs.Bar;
                                break;
                            case "Bubble":
                                dataSeries.RenderAs = RenderAs.Bubble;
                                break;
                            case "Column":
                                dataSeries.RenderAs = RenderAs.Column;
                                break;
                            case "Doughnut":
                                dataSeries.RenderAs = RenderAs.Doughnut;
                                break;
                            case "Line":
                                dataSeries.RenderAs = RenderAs.Line;
                                break;
                            case "Pie":
                                dataSeries.RenderAs = RenderAs.Pie;
                                break;
                            case "Point":
                                dataSeries.RenderAs = RenderAs.Point;
                                break;
                            case "StackedArea":
                                dataSeries.RenderAs = RenderAs.StackedArea;
                                break;
                            case "StackedBar":
                                dataSeries.RenderAs = RenderAs.StackedBar;
                                break;
                            case "StackedColumn":
                                dataSeries.RenderAs = RenderAs.StackedColumn;
                                break;
                        }
                                                                       
                        
                        dataSeries.Name = strChartSeriesName.Split('µ')[i];
                        dataSeries.LegendText = strChartSeriesName.Split('µ')[i];

                        int nbrData = strChartData.Split('µ').Count();

                        int l = 0;
                        if (intNbrSeries >= 2)
                        {
                            string strCurrentData = strChartData.Split('µ')[i];

                            //for (int k = 0; k < nbrData; k++)
                            //{                                
                            foreach (string strDataPoint in strCurrentData.Split('¤'))
                            {
                                if (strDataPoint != "")
                                {
                                    DataPoint dataPoint;
                                    dataPoint = new DataPoint();
                                    dataPoint.YValue = Convert.ToDouble(strDataPoint);
                                    dataPoint.AxisXLabel = strChartDataName.Split('µ')[l];
                                    dataSeries.DataPoints.Add(dataPoint);
                                    l++;
                                }
                            }

                            //}
                            BlackChart.Series.Add(dataSeries); 
                        }

                        if (intNbrSeries < 2)
                        {
                            

                            for (int k = 0; k < nbrData; k++)
                            {
                            string strCurrentData = strChartData.Split('µ')[k];
                            foreach (string strDataPoint in strCurrentData.Split('¤'))
                            {
                                if (strDataPoint != "")
                                {
                                    DataPoint dataPoint;
                                    dataPoint = new DataPoint();
                                    dataPoint.YValue = Convert.ToDouble(strDataPoint);
                                    dataPoint.AxisXLabel = strChartDataName.Split('µ')[k];
                                    dataSeries.DataPoints.Add(dataPoint);
                                }
                            }

                            }
                            BlackChart.Series.Add(dataSeries); 
                        }
                           
                        
                    }

                    BlackChart.MinHeight = 80;
                    this.BlackLightHost.AddPanel(new Blacklight.Silverlight.Controls.DragDockPanel()
                    {
                        MinHeight = 80,
                        Margin = new Thickness(15),
                        Header = strChartSetup.Split('µ')[0],
                        Content = BlackChart,
                        
                    });
                }
            }

                    

                    // Add chart to LayoutRoot

                    
                }
                
            }
        
        }           
